﻿using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using EasyXaf.EntityDesigner.BusinessObjects.Entities;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.EntityProperties;
using System.ComponentModel;

namespace EasyXaf.EntityDesigner.BusinessObjects.Entities.Associations;

/// <summary>
/// 关系
/// </summary>
[XafDisplayName("关系")]
[XafDefaultProperty(nameof(Name))]
[DeferredDeletion(false)]
public partial class Association : BaseObject
{
	private string _name;
	private Entity _parent;
	private EntityProperty _parentProperty;
	private Entity _child;
	private EntityProperty _childProperty;
	private AssociationType _associationType;
	private bool _isAggregated;
	private Entity _entity;

	/// <summary>
	/// 名称
	/// </summary>
	[XafDisplayName("名称")]
	[RuleRequiredField]
	public string Name
	{
		get => _name;
		set => SetPropertyValue(nameof(Name), ref _name, value);
	}

	/// <summary>
	/// 父实体
	/// </summary>
	[XafDisplayName("父实体")]
	[DataSourceProperty(nameof(EntityDataSource))]
	public Entity Parent
	{
		get => _parent;
		set => SetPropertyValue(nameof(Parent), ref _parent, value);
	}

	/// <summary>
	/// 父实体属性
	/// </summary>
	[XafDisplayName("父实体属性")]
	[DataSourceProperty(nameof(ParentPropertyDataSource))]
	public EntityProperty ParentProperty
	{
		get => _parentProperty;
		set => SetPropertyValue(nameof(ParentProperty), ref _parentProperty, value);
	}

	/// <summary>
	/// 父实体属性名
	/// </summary>
	[XafDisplayName("父实体属性名")]
	[Browsable(false)]
	public string ParentPropertyName
	{
		get => GetParentPropertyName();
	}

	/// <summary>
	/// 子实体
	/// </summary>
	[XafDisplayName("子实体")]
	[DataSourceProperty(nameof(EntityDataSource))]
	public Entity Child
	{
		get => _child;
		set => SetPropertyValue(nameof(Child), ref _child, value);
	}

	/// <summary>
	/// 子实体属性
	/// </summary>
	[XafDisplayName("子实体属性")]
	[DataSourceProperty(nameof(ChildPropertyDataSource))]
	public EntityProperty ChildProperty
	{
		get => _childProperty;
		set => SetPropertyValue(nameof(ChildProperty), ref _childProperty, value);
	}

	/// <summary>
	/// 子实体属性名
	/// </summary>
	[XafDisplayName("子实体属性名")]
	[Browsable(false)]
	public string ChildPropertyName
	{
		get => GetChildPropertyName();
	}

	/// <summary>
	/// 关系类型
	/// </summary>
	[XafDisplayName("关系类型")]
	public AssociationType AssociationType
	{
		get => _associationType;
		set => SetPropertyValue(nameof(AssociationType), ref _associationType, value);
	}

	/// <summary>
	/// 聚合
	/// </summary>
	[XafDisplayName("聚合")]
	public bool IsAggregated
	{
		get => _isAggregated;
		set => SetPropertyValue(nameof(IsAggregated), ref _isAggregated, value);
	}

	/// <summary>
	/// 实体
	/// </summary>
	[XafDisplayName("实体")]
	[Association("Associations-Entity")]
	public Entity Entity
	{
		get => _entity;
		set => SetPropertyValue(nameof(Entity), ref _entity, value);
	}

	/// <summary>
	/// IsValid
	/// </summary>
	[Browsable(false)]
	[RuleFromBoolProperty(UsedProperties = "Parent,Child", CustomMessageTemplate = "父实体或子实体至少一个为当前实体")]
	public bool IsValid
	{
		get => GetIsValid();
	}

	[Browsable(false)]
	public IList<Entity> EntityDataSource
	{
		get => GetEntityDataSource();
	}

	[Browsable(false)]
	public IList<EntityProperty> ParentPropertyDataSource
	{
		get => GetParentPropertyDataSource();
	}

	[Browsable(false)]
	public IList<EntityProperty> ChildPropertyDataSource
	{
		get => GetChildPropertyDataSource();
	}

	public Association(Session session)
		: base(session)
	{
	}

	private partial string GetParentPropertyName();

	private partial string GetChildPropertyName();

	private partial bool GetIsValid();

	private partial IList<Entity> GetEntityDataSource();

	private partial IList<EntityProperty> GetParentPropertyDataSource();

	private partial IList<EntityProperty> GetChildPropertyDataSource();
}
